<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Issue;
use App\Models\Article;
use App\Models\Author;
use App\Models\ArticleCat;

class IssueController extends Controller
{
    public function current()
    {
        $issues = Issue::all();
        $issue = [];
        foreach ($issues as $key => $value) {
            $issue[$value->id] = $value['year'] .'-'. $value['volume'] .'-'. $value['issue'];
        }

        $issue_value = max($issue);
        $issue_key = array_keys($issue, max($issue));
        $id = $issue_key[0];
        return $this->index($id);
    }


    public function index($issue)
    {
        $id = $issue;
        $issue = Issue::find($id);
        // $journal_id = $issue['journal_id'];
        $articles = Article::where('issue_id', $issue->id)->get();
        $articlesNew =[];
        $defaultCate = ArticleCat::ARTICLE_ID;
        foreach ($articles as $article) {
            if (!$article->cate_id) {
                $article->cate_id = $defaultCate;
            }
            $articlesNew[$article->articleCat->name][] = $article;
        }
        return view('issue.index', ['issue' => $issue, 'articles'=> $articlesNew]);
    }

}
